/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package heartinjava;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
/**
 *
 * @author TIENDAT
 */

public class XLWaiting extends KhoiTao {
    
    public XLWaiting(final HeartInJava gameControl, final KhungBanCo gui) 
    {
        this.m_Main= gameControl;
        this.gui = gui;
    }

    @Override
    public void Update() 
    {
        if (m_Main.getType() == KhaiBao.ISSERVER) 
        {
            if (m_Main.getServer().getClientCount() == 3) 
            {
                m_Main.setViTri(0);
                m_Main.getServer().SendToClient(0, "start1");
                m_Main.getServer().SendToClient(1, "start2");
                m_Main.getServer().SendToClient(2, "start3");
                m_Main.SwitchState(KhaiBao.PLAY);
            }

        }
        else if (m_Main.getType() == KhaiBao.ISCLIENT) 
        {    
        }
    }

    @Override
    public void Enter()
    {
        gui.container.removeAll();

        URL path = getClass().getResource("resources/bg1_1.jpg");
        JLabel bg= new JLabel(new ImageIcon(path));
        bg.setBounds(gui.container.getX(), gui.container.getX(),gui.container.getWidth(), gui.container.getHeight());
        gui.container.setBackground(Color.WHITE);
        gui.container.add(bg);

        // them button New Game
//        JButton btnWait = new JButton("Vui lòng chờ ...");
//        btnWait.setBounds(330, 200, 150, 30);
//        btnWait.setEnabled(false);
//        btnWait.addActionListener(new ActionListener() {
//
//            public void actionPerformed(ActionEvent e) {
//                //gameControl.SwitchState(KhaiBao.WAIT);
//            }
//        });
//        gui.container.add(btnWait,0);

        if (m_Main.getType() == KhaiBao.ISSERVER)
        {
            Font f1 = new Font(Font.DIALOG, Font.BOLD, 16);
            JLabel ip = new JLabel();
            ip.setForeground(Color.BLUE);
            ip.setText("Server name : " + m_Main.getServer().getIpAddress().toString());
            ip.setFont(f1);
            ip.setBounds(290, 150, 350, 30);
            gui.container.add(ip,0);
        }

        // them button Done
        JButton Done = new JButton("Done");
        Done.setBounds(370, 380, 150, 30);
        Done.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
                if(m_Main.getType()==KhaiBao.ISCLIENT)
                {
                    m_Main.getClient().stopThread();
                }
                else if(m_Main.getType() == KhaiBao.ISSERVER)
                {
                    m_Main.getServer().SendToAllClient("exit");
                    m_Main.getServer().stop();
                }
                m_Main.SwitchState(KhaiBao.MENU);
            }
        });
        gui.container.add(Done,0);
        gui.repaint();
    }
    
    @Override
    public void HaveMessage(String msg){
        if(m_Main.getType() == KhaiBao.ISCLIENT && msg.startsWith("start")){
            int vitri = Integer.parseInt(msg.split("start")[1]);
            m_Main.setViTri(vitri);
            m_Main.SwitchState(KhaiBao.PLAY);
        }
    }
}
